package com.mzkj.wanjulaile.mobile.api.modules.coupon.service;

import com.mzkj.common.utils.StringUtils;
import com.mzkj.wanjulaile.mobile.api.common.service.CrudService;
import com.mzkj.wanjulaile.mobile.api.modules.account.entity.VipScheme;
import com.mzkj.wanjulaile.mobile.api.modules.coupon.entity.AccountCoupon;
import com.mzkj.wanjulaile.mobile.api.modules.coupon.entity.Coupon;
import com.mzkj.wanjulaile.mobile.api.modules.coupon.mapper.AccountCouponMapper;
import com.mzkj.wanjulaile.mobile.api.modules.coupon.mapper.InviteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 会员优惠券Service
 * @author hgw
 * @version 2016-11-11
 */
@Service
@Transactional(readOnly = true)
public class AccountCouponServiceImpl  implements AccountCouponService {

	@Autowired
	private InviteMapper inviteMapper;

	@Autowired
	private AccountCouponMapper accountCouponMapper;

	public AccountCoupon get(String id) {
		AccountCoupon accountCoupon = accountCouponMapper.get(id);
		return accountCoupon;
	}
	
	public List<AccountCoupon> findList(AccountCoupon accountCoupon) {
		return accountCouponMapper.findList(accountCoupon);
	}

	
	@Transactional(readOnly = false)
	public void save(AccountCoupon accountCoupon) {
		if (StringUtils.isBlank(accountCoupon.getId())){
			accountCoupon.setIsNewRecord(true);
			accountCoupon.preInsert();
			accountCouponMapper.insert(accountCoupon);
		}else{
			accountCoupon.preUpdate();
			accountCouponMapper.update(accountCoupon);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(AccountCoupon accountCoupon) {
		accountCouponMapper.delete(accountCoupon);
	}

	//获取优惠券列表
	public List<AccountCoupon> getCouponsByAccountId(AccountCoupon accountCoupon){
		return accountCouponMapper.getCouponsByAccountId(accountCoupon);
	}

	@Transactional(readOnly = false)
	public void insert(AccountCoupon accountCoupon){
		accountCouponMapper.insert(accountCoupon);
	}

	/**
	 * 获取会员费用抵用券数量
	 * @param accountCoupon
	 * @return
     */
	public int couponCount(AccountCoupon accountCoupon){
		return accountCouponMapper.couponCount(accountCoupon);
	}

	public AccountCoupon getById(AccountCoupon accountCoupon){
		return accountCouponMapper.getById(accountCoupon);
	}
	/**
	 * 获取不满足条件的优惠券列表
     */
	public List<AccountCoupon> getUnUseCoupon(AccountCoupon accountCoupon){
		return accountCouponMapper.getUnUseCoupon(accountCoupon);
	}

	@Override
	public List<AccountCoupon> getUseCoupon(AccountCoupon accountCoupon) {
		return accountCouponMapper.getUseCoupon(accountCoupon);
	}

	/**
	 * 根据购买的会员制度获取优惠券的减免金额
	 *
	 * @param accountCouponId
	 * @return
	 */
	@Override
	public int getCouponVal(String accountCouponId, VipScheme vipScheme) {
		AccountCoupon accountCoupon1 = accountCouponMapper.getById(new AccountCoupon(accountCouponId));
		Coupon coupon = accountCoupon1.getCoupon();
		int faceValue = 0;
		if (coupon != null) {
			if (coupon.getCouponType().equals("1")) {
				//会员购买抵用券
				if (coupon.getCouponMode() == 1) {
					//满减券
					if (vipScheme.getFee() > (coupon.getMinAmount() == null ? 0 : coupon.getMinAmount())) {
						faceValue = Integer.valueOf(coupon.getFaceValue());
					}
				} else if (coupon.getCouponMode() == 2) {
					//折扣券
					int discountFee = vipScheme.getFee() - vipScheme.getFee() * coupon.getDiscount() / 100;
					if (coupon.getMaxDiscountMoney() != null && discountFee > coupon.getMaxDiscountMoney()) {
						faceValue = Integer.valueOf(coupon.getMaxDiscountMoney());
					} else {
						faceValue = discountFee;
					}
				}
			}
		}
		return faceValue;
	}

	@Override
	public List<AccountCoupon> newFindList(AccountCoupon accountCoupon) {
		return accountCouponMapper.newFindList(accountCoupon);
	}
}